<template>
	<div>

		<el-dialog title="角色息修改" :visible.sync="updateModelVisible">
			<el-form label-width="100px">
				<el-form-item label="角色名">
					<el-input v-model="updateRole.roleName" placeholder="请输入内容" :disabled="true"></el-input>
				</el-form-item>
				<el-form-item label="角色描述">
					<el-input v-model="updateRole.roleNickname" placeholder="请输入内容"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="updateRoleInfo">修改</el-button>
			</div>
		</el-dialog>




		<el-dialog title="新增角色" :visible.sync="isAddVisible">
			<el-form label-width="100px">
				<el-form-item label="角色名">
					<el-input v-model="addNewRoler.roleName" placeholder="请输入内容"></el-input>
				</el-form-item>
				<el-form-item label="角色描述">
					<el-input v-model="addNewRoler.roleNickname" placeholder="请输入内容"></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="clearAddForm">重置</el-button>
				<el-button type="primary" @click="addRoler">添加</el-button>
			</div>
		</el-dialog>




		<el-dialog title="授权" :visible.sync="addModel">

			<el-tree ref="treeMenu" :data="menusRoles" show-checkbox node-key="menuId" :props="defaultProps"
				:default-checked-keys="checks">
			</el-tree>



			<div slot="footer" class="dialog-footer">
				<!-- <el-button>重置</el-button> -->
				<el-button type="primary" @click="saveRoleMenus">授权</el-button>
			</div>
		</el-dialog>





		<div id="">


			<div class="searchWord">

				<el-form :inline="true" class="demo-form-inline">

					<el-form-item label="模糊条件">
						<el-input v-model="findParam.key" style="width: 300px;" class='searchs' placeholder="请输入搜索内容">
						</el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" icon="el-icon-search" round @click="search()">查询</el-button>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" plain @click="oppenRoleModel()">新增角色</el-button>
					</el-form-item>
					<el-form-item>
						<el-button type="danger" icon="el-icon-delete" circle :disabled="this.deleIds.length === 0"
							@click="deleteRoles"></el-button>
					</el-form-item>
				</el-form>

			</div>


			<el-table :data="roles" style="width: 100%" border @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55">
				</el-table-column>

				<el-table-column prop="roleName" label="角色名" width="300">
				</el-table-column>

				<el-table-column prop="roleNickname" label="权限判定" width="300">
				</el-table-column>


				<el-table-column fixed="right" label="操作" width="300">
					<template slot-scope="scope">
						<el-button type="warning" plain @click="openUpdateRoleModel(scope)">修改</el-button>
						<el-button type="primary" @click="authorization(scope)">授权</el-button>
					</template>

				</el-table-column>
			</el-table>
		</div>
		<div id="">
			<el-pagination :current-change="findParam.indexPage" background layout="sizes,prev, pager, next, jumper"
				:total="total" :page-sizes="size" :page-size="findParam.pageSize" @current-change="changePage"
				@size-change="changePageSize">
			</el-pagination>


		</div>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				updateModelVisible: false,
				isAddVisible: false,
				addNewRoler: {
					roleName: null,
					roleNickname: null
				},
				findParam: {
					key: null,
					indexPage: 1, //当前页码
					pageSize: 5, //当前页码能看几条
					// 给他一个默认值可以修改
				},
				roles: [],
				total: 0,
				size: [3, 5, 10, 20, 30],
				addModel: false,
				defaultProps: {
					children: 'listMenus',
					label: 'menuName'
				},
				checks: [],
				menusRoles: [],
				addrolesMenus: {
					roleId: null,
					checkIds: []
				},
				deleIds: [],
				updateRole: []
			}

		},
		methods: {
			updateRoleInfo() {
				this.$http.post('/role-table/updateRole.do', this.updateRole)
					.then(resp => {
						if (resp.data.code == 200) {

							this.$message({
								type: 'success',
								message: '修改成功 '
							});
							this.listRoles();
							this.updateModelVisible = false;

							//初始化勾选
							// this.dele.ids = [];

							this.updateRole = [];
						} else {
							this.$message({
								type: 'error',
								message: '修改失败 ' + resp.data.msg
							});
						}

					})



			},
			openUpdateRoleModel(scope) {
				//获取最新的scope
				this.updateModelVisible = true;


				this.$http.get("/role-table/findRoleById/" + scope.row.roleId).then(resp => {
					if (resp.data.code == 200) {
						let obj = resp.data.data;
						this.updateRole = resp.data.data;

					} else {
						this.$message({
							type: 'error',
							message: '查询数据失败 '
						});

					}
				})


			},
			clearAddForm() {
				this.addNewRoler = {
					roleName: null,
					roleNickname: null
				}
			},
			addRoler() {
				this.$http.post('/role-table/SaveRole.do', this.addNewRoler)
					.then(resp => {
						if (resp.data.code == 200) {
							this.$message({
								type: 'success',
								message: '添加成功 '
							});
							this.isAddVisible = false;
							this.listRoles();
							// this.dele.ids = [];
							this.clearAddForm()
						} else {
							this.$message({
								type: 'error',
								message: '添加失败 ' + resp.data.msg
							});

						}

					})
			},
			oppenRoleModel() {
				this.isAddVisible = true;
			},
			handleSelectionChange(val) {
				//获取勾选的内容

				//初始化
				this.deleIds = [];
				for (let i = 0; i < val.length; i++) {
					this.deleIds.push(val[i].roleId);
				}
			},
			deleteRoles() {
				this.$http.post('/role-table/removeRoleByIds.do', this.deleIds)
					.then(resp => {
						if (resp.data.code == 200) {

							this.$message({
								type: 'success',
								message: '删除成功 '
							});
							this.listRoles();
							//初始化勾选
							// this.deleIds = [];
						} else {
							this.$message({
								type: 'error',
								message: '删除失败 ' + resp.data.msg
							});
						}

					})
			},
			search() {
				this.listRoles();
			},
			//加载用户
			listRoles() {
				this.$http.post("/role-table/listRoles.do", this.findParam)
					.then(resp => {
						if (resp.data.code == 200) {
							this.roles = resp.data.data.list;
							this.total = resp.data.data.total;
							this.findParam.indexPage = resp.data.data.indexPage;
							this.deleIds = [];
						}

					})
			},
			changePage(page) {
				this.findParam.indexPage = page;
				this.listRoles();
			},
			changePageSize(size) {
				this.findParam.pageSize = size;
				this.listRoles();
			},
			authorization(scope) {
				this.addModel = true;
				this.checks = [];
				this.menusRoles = [];
				this.addrolesMenus.roleId = scope.row.roleId;
				this.$http.post("/menu-table/listMenusByRoleId.do/" + scope.row.roleId)
					.then(resp => {
						if (resp.data.code == 200) {
							this.checks = resp.data.data.ids;
							this.menusRoles = resp.data.data.list;
						}
					})
			},
			saveRoleMenus() {
				// let checks = this.$refs['treeMenu'].getCheckedNodes(false, true);

				this.addrolesMenus.checkIds = this.$refs['treeMenu'].getCheckedKeys().concat(this.$refs['treeMenu']
					.getHalfCheckedKeys())

				//开始提交代码

				this.$http.post("/role-menu/saveRoleMenus.do", this.addrolesMenus).then(resp => {
					if (resp.data.code == 200) {
						this.$message({
							type: 'success',
							message: '授权成功 '
						});
						this.listRoles();
						this.addModel = false;
					} else {
						this.$message({
							type: 'error',
							message: '授权失败 '
						});
					}
				})

			}

		},
		created() {
			this.listRoles();

		}
	}
</script>

<style scoped>

</style>
